﻿/*!
* jQuery grid plugin: 
* 
* version 0.1 2011-03-10
* Requires jQuery v1.4.2 or later
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Authors: Kim Seoung Min
* Company : Kongyoung DB
*/

(function ($) {
    $.fn.superboard = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };
    
    var MaincontextMenu = {
        Menu: [
                            { name: 'add3', cmd: 'ADD', img: '/image/ico_email.gif', href: 'addadd' },
                            { name: 'view3', cmd: 'VIEW', img: '/image/ico_email.gif', href: 'viewview' }
                        ],
        Click: function (event, ui) {
            if ($(ui).attr("cmd") == "ADD") {
                event.data.obj.superboard("Add", "uid", "titADD", "Board", 1, 100);
            }

            else {
                alert($(ui).attr("cmd") + " - 클래스내부의 메인단축메뉴가 실행되었습니다");
            }
        }
    };
    
    var UicontextMenu = {
        Menu: [
                            { name: 'add30', cmd: 'ADD', img: '/image/ico_email.gif', href: 'addadd' },
                            { name: 'Delete', cmd: 'DELETE', img: '/image/ico_email.gif', href: 'viewview' }
        ],

        Click: function (event, ui) {
            if ($(ui).attr("cmd") == "DELETE") {
                uid = $(event.data.obj).attr("uid");
                try {
                    var pl = new JSONClientParameters();
                    pl.add("sb_uid", uid);
                    PostJsonData("/bzService/superboard.asmx/Delete", pl, function (jdata) {
                        event.data.obj.remove();
                    });
                }
                catch (e) {
                    SOAPError(e.message);
                }
            }
            else {
                alert($(ui).attr("cmd") + " - 아이콘단축메뉴가 실행되었습니다");
            }
        }

    };

    var boardmenu = {
        colNames: ['Id', 'Votes', 'Title'],
        MaincontextMenu: [
                        { name: 'add2', cmd: 'ADD', img: '/image/ico_email.gif', href: 'addadd' },
                        { name: 'view2', cmd: 'VIEW', img: '/image/ico_email.gif', href: 'viewview' }
                    ],
        UicontextMenu: [
            { name: 'Uiadd', cmd: 'ADD', img: '/image/ico_email.gif', href: 'addadd' },
            { name: 'UIview', cmd: 'VIEW', img: '/image/ico_email.gif', href: 'viewview' }
        ],
        rowNum: 20,
        ShowRowNum: true,
        caption: "JSON Example"
    };

    var methods = {
        init: function () {
            var el = $(this);
            // $(this).resizable();
            $(this).addClass("dashboard-main");
            // $(this).bind("contextmenu", function(e) {return false;});
            $(this).supercontextMenu(MaincontextMenu);
            $(this).droppable({
                drop: function (event, ui) {
                    var ukey = ui.draggable.attr('UKEY');
                    if (ukey != null) {
                        _left = ui.offset.left - $(this).offset().left;
                        _top = ui.offset.top - $(this).offset().top;
                        $(this).superboard("Create", ukey, ukey, "Board", _left, _top);
                        ui.draggable.hide();
                    }
                }
            });
        },

        Create: function (uid, title, body, x, y) {

            try {
                $(this).superboard("Add", uid, title, body, x, y);
                var pl = new JSONClientParameters();
                pl.add("sb_uid", uid);
                pl.add("sb_name", title);
                pl.add("sb_x", x);
                pl.add("sb_y", y);
                pl.add("sb_body", body);
                PostJsonData("/bzService/superboard.asmx/Create", pl, function (jdata) {
                    //   $(this).superboard("Add", uid, title, body, x, y);    
                    //   alert(jdata);
                });
            }
            catch (e) {
                SOAPError(e.message);
            }
        },

        Add: function (uid, title, body, x, y) {
            $(this).append(String.format("<div class='dashboard-ui' uid='{2}'><h3 class='ui-widget-header'>{0}</h3><a>{1}</a><br /></div>", title, body, uid));

            var el = $(this).find('div:last');

            this.superboard("SetDrag", el);
            //el.css("left", x);
            //el.css("top", y);
            x = parseFloat(x) + el.parent().offset().left;
            y = parseFloat(y) + el.parent().offset().top;
            el.offset({ top: y, left: x });
            el.draggable("option", "stack", $(this).find('div'));
            el.supercontextMenu(UicontextMenu);
        },

        Delete: function (uid) {

            try {
                var pl = new JSONClientParameters();
                pl.add("sb_uid", uid);
                PostJsonData("/bzService/superboard.asmx/Delete", pl, function (jdata) {
                    $(this).remove(String.format("div[uid='{0}']", uid));
                });
            }
            catch (e) {
                SOAPError(e.message);
            }

        },

        userClick: function (userFun) {
            MaincontextMenu.Click = userFun;
        },

        DragStop: function (obj) {
            var _left = $(obj).offset().left - $(obj).parent().offset().left;
            var _Top = $(obj).offset().top - $(obj).parent().offset().top;
            var uid = obj.attr("uid");
            try {
                var pl = new JSONClientParameters();
                pl.add("sb_uid", uid);
                pl.add("sb_x", _left);
                pl.add("sb_y", _Top);
                PostJsonData("/bzService/superboard.asmx/Move", pl, function (jdata) {
                }
                );
            }
            catch (e) {
                SOAPError(e.message);
            }

        },

        SetDrag: function (obj) {
            $(obj).draggable({
                start: function () {
                    //   alert("출발");
                },
                drag: function () {
                },
                stop: function () {
                    $(this).parent().superboard("DragStop", $(this));
                }
            });

            $(obj).draggable("option", "containment", $(this));
            $(obj).mouseover(function () { $(this).addClass("dashboard-ui-mouseover") }).mouseout(function () { $(this).removeClass("dashboard-ui-mouseover") });

        },

        GetRowCount: function (options) {
            alert(options + "-" + "옵션입니다");
            return this;

        }

    };


    function MainContextMenuClick(e) {
        var _cmd = $(this).attr('cmd');
        if (_cmd == 'ADD') {
            var ed = e.data.obj;
            ed.superboard("Add", "uid", "titADD", "Board", 1, 100);
        }
    }

    function UiContextMenuClick(e) {
        var _cmd = $(this).attr('cmd');
        var _uid = e.data.obj.attr('uid');
        alert(_uid + ' / ' + _cmd);
        this.superboard("Delete", _uid);
    }

})(jQuery);

String.format = function(text) {
    if (arguments.length <= 1) {
        return text;
    }
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }
    return text;
};


$.extend($.expr[':'], {
    focusable: function(element) {
        var nodeName = element.nodeName.toLowerCase(),
		tabIndex = $.attr(element, 'tabindex');

        return (/input|select|textarea|button|object/.test(nodeName)
			? !element.disabled
			: 'a' == nodeName || 'area' == nodeName
				? element.href || !isNaN(tabIndex)
				: !isNaN(tabIndex))

        // the element and all of its ancestors must be visible
        // the browser may report that the area is hidden
		&& !$(element)['area' == nodeName ? 'parents' : 'closest'](':hidden').length;
    }
});

// 내  용 : 왼쪽부터 string 잘라내기
// 작성자 : 최보현(bhchoi@00db.co.kr)
// 작성일 : 2011/01/11
function Left(String, Length)
{
	if (String == null)
		return (false);

	return String.substr(0, Length);
}

